<?php
global $m, $tab, $a;
require_once($AppUI->getModuleClass('projects'));
require_once($AppUI->getMasterDataClass('mdtype'));
require_once($AppUI->getMasterDataClass('mdstatus'));

$perm = &$AppUI->acl();
if (!$perm->userHasRole($AppUI->user_id, 'Accounting')
&& !$perm->userHasRole($AppUI->user_id, 'Marketing')
&& !$perm->userHasRole($AppUI->user_id, 'FFIA_Management')
&& !$perm->userHasRole($AppUI->user_id, 'FFIA_Staff')
&& !$perm->userHasRole($AppUI->user_id, 'Operation')
&& !$perm->userHasRole($AppUI->user_id, 'General_Management')) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect("m=home&tab=0");
	exit;
}

$page_offset = intval( dPgetParam( $_GET, 'page_offset', 0 ) );
$page_limit = intval( dPgetParam( $_POST, 'page_limit', 10000000));
$customer = intval( dPgetParam( $_POST, 'project_company', 0 ) );
$status_id = intval( dPgetParam( $_POST, 'project_rstatus', 0 ) );
$keyword = dPgetParam( $_POST, 'keyword', '' );

$canViewSalesAmount = $perm->checkModuleItem("taskfee", "view");
$canEditSalesAmount = $perm->checkModuleItem("taskfee", "edit");
$canViewExpense = $perm->checkModuleItem('task_expense', 'view');
$canDelete = $perm->checkModuleItem("projects", "delete");
// Load Object Status
$objMD = new CMdStatus();
$statList = NULL;
$hasRoleAccounting = $perm->userHasRole($AppUI->user_id, "Accounting");
if ($hasRoleAccounting) {
	$complete_status = new CMDStatus();
	$project_completed = dPgetSysVal( 'ProjectCompleted' );
	$complete_status->loadByCode($project_completed[0]);
	$statList = array($complete_status->status_id => $complete_status->status_name);
} else {
	if (count($_POST) == 0 && $status_id <= 0 && ($perm->userHasRole($AppUI->user_id, 'General_Management') || $perm->userHasRole($AppUI->user_id, 'FFIA_Management') || $perm->userHasRole($AppUI->user_id, 'FFIA_Staff'))) {
		$open_status = new CMDStatus();
		$project_open = dPgetSysVal( 'ProjectOpen' );
		$open_status->loadByCode($project_open[0]);
		$status_id = $open_status->status_id;
		$_POST['project_rstatus'] = $status_id;
	}
	$statList = $objMD->getStatusList('Reference');
}

$pro = new CProject();
$totalRows = 0;
$projects = $pro->getList($_POST, $totalRows, $page_limit, $page_offset);

$list = array();

header('Content-Type: text/xml');


$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('root');
$xml->startElement('projects');

//	obj2xml($xml, 'projects', 'item', $list, 'project_id', 'project_iso_code');
foreach($projects as $row) {
	$row['project_percent_complete'] = $pro->getCompletedPercent($row['project_id']);
	$row['sa_expense_amount'] = $pro->getExpenseAmount($row['project_id']);
	$row['has_task_or_task_log'] = $pro->hasTaskOrTaskLog($row['project_id']) ? 'true' : 'false';
	
	$xml->startElement('item');
	foreach($row as $key => $value) {
        if ($key == $key_field) {
	        $xml->writeElement('data', $value);
        }
        else if ($key == $label_field) {
	        $xml->writeElement('label', $value);
        }
		$xml->writeElement($key, $value);
	}
   	$xml->endElement();
}

$xml->endElement();
$xml->endElement();
echo $xml->outputMemory(true);
?>